﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using iShoppingDTO;

namespace iShoppingDAO
{
    public class PhanQuyenDAO
    {
        public static DataTable SelectAllQuyen()
        {
            DataTable dt = new DataTable();
            try
            {

                string sql = "SELECT * FROM PhanQuyen";
              
                dt = DataProvider.ExecuteQuery(sql);

            }
            catch (Exception ex)
            {
                dt = new DataTable();
            }
            return dt;
        }

        public static bool InsertPhanQuyen(PhanQuyenDTO phanQuyenDto)
        {
            bool res = false;
            try
            {
                string sql = "INSERT INTO PhanQuyen(MaPhanQuyen,TenQuyen) VALUES(@MaPhanQuyen,@TenQuyen)";
                List<SqlParameter> parameters = new List<SqlParameter>();

                SqlParameter[] par = new SqlParameter[2];
                par[0] = new SqlParameter("@MaPhanQuyen", SqlDbType.NVarChar);
                par[0].Value = phanQuyenDto.MaPhanQuyen;
                parameters.Add(par[0]);
                par[1] = new SqlParameter("@TenQuyen", SqlDbType.NVarChar);
                par[1].Value = phanQuyenDto.TenQuyen;
                parameters.Add(par[1]);

                res = DataProvider.ExecuteNoneQuery(sql, parameters);

            }
            catch (Exception)
            {
                res = false;
            }
            return res;
        }

        public static bool DeleteQuyenofMa(string maQuyen)
        {
            bool res = false;
            try
            {
                string sql = "DELETE FROM PhanQuyen WHERE MaPhanQuyen = @MaPhanQuyen";
                List<SqlParameter> parameters = new List<SqlParameter>();

                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaPhanQuyen", SqlDbType.NVarChar);
                par[0].Value = maQuyen;
                parameters.Add(par[0]);
            
                res = DataProvider.ExecuteNoneQuery(sql, parameters);

            }
            catch (Exception)
            {
                res = false;
            }
            return res;
        }

        public static DataTable SelectOfQuyen(string maLoaiTaiKhoan)
        {
            DataTable dt = new DataTable();
            try
            {

                string sql = "SELECT * FROM PhanQuyen WHERE MaPhanQuyen = " + maLoaiTaiKhoan;

                dt = DataProvider.ExecuteQuery(sql);

            }
            catch (Exception ex)
            {
                dt = new DataTable();
            }
            return dt;
        }
    }
}
